package utils;

/**
 * @Package: utils
 * @Description 根据身份证前十七位生成对应最后一位的校验码
 * @Author xiWei
 * @WeChat XW-MXN
 * @Date 2024/10/10 17:23
 * @Version V2.0.0
 */
public class creatID {

    public static void main(String[] args) {
        String id17 = "41142519930413391"; // 示例：前17位数字
        char checkDigit = calculateCheckDigit(id17);
        System.out.println("完整的身份证号码为：" + id17 + checkDigit);
    }

    public static char calculateCheckDigit(String id17) {
        if (id17 == null || id17.length() != 17) {
            throw new IllegalArgumentException("输入的前17位身份证号码格式不正确");
        }

        int[] weights = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        int sum = 0;

        for (int i = 0; i < 17; i++) {
            int num = Character.digit(id17.charAt(i), 10);
            sum += num * weights[i];
        }

        int mod = sum % 11;
        char[] checkCodes = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

        return checkCodes[mod];
    }
}
